<?php

namespace App\Http\Controllers\IndexManage;

use App\Model\Biji;
use App\Facades\AjaxResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BijiController extends Controller{

	/**
	 * 根据用户ID获取笔记列表
	 *
	 * @param Request $request
	 * @return mixed
	 */
    public function index(Request $request){
    	$user_id = session('user_id');
		$validator = Validator::make($request->all(), [
			'bijis_sort' 	=> 'required|string'
		]);
		if($validator->fails()){
			return AjaxResponse::fail(AJAX_CODE_REQUEST_PARAMS_ERROR, $validator->errors()->first());
		}
    	if($user_id){
			$request['bijis_sort'] = $request->input('bijis_sort');
			$select_fields = array(
				Biji::FIELD_BIJI_ID,
				Biji::FIELD_TAG_ID,
				Biji::FIELD_BIJI_TITLE,
				Biji::FIELD_BIJI_CONTENT,
				Biji::FIELD_CREATED_TIME,
				Biji::FIELD_UPDATED_TIME,
			);
			$return['biji_list'] = Biji::getBijisByUserId($user_id, $request['bijis_sort'], $select_fields);
			return AjaxResponse::success($return);
		}else{
    		return AjaxResponse::fail();
		}
    }
}
